<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Forpastime <forpastime@qq.com>
// +----------------------------------------------------------------------
namespace plugins\wxapp_broadcast\controller; 

use cmf\controller\PluginAdminBaseController;
use think\Db;
use think\db\Query;
use think\facade\Request;
use plugins\wxapp_broadcast\model\WxappBroadcastRoomModel;

class AdminIndexController extends PluginAdminBaseController
{
    public function _initialize()
    {
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        } else {
            $this->error('未登录');
        }
    }

    public function index()
    {
        $param = $this->request->param();

        $room = new WxappBroadcastRoomModel();

        $rooms = $room->all();
        $this->assign('rooms',$rooms);
        return $this->fetch();
    }

    /**
     * 创建直播间
     * name: "测试直播房间1",  // 房间名字
     * coverImg: "",   // 通过 uploadfile 上传，填写 mediaID
     * startTime: 1588237130,   // 开始时间
     * endTime: 1588237130 , // 结束时间
     * anchorName: "zefzhang1",  // 主播昵称
     * anchorWechat: "WxgQiao_04",  // 主播微信号
     * subAnchorWechat: "WxgQiao_03",  // 主播副号微信号
     * createrWechat: 'test_creater', // 创建者微信号
     * shareImg: "" ,  //通过 uploadfile 上传，填写 mediaID
     * feedsImg: "",   //通过 uploadfile 上传，填写 mediaID
     * isFeedsPublic: 1, // 是否开启官方收录，1 开启，0 关闭
     * type: 1 , // 直播类型，1 推流 0 手机直播
     * screenType: 0,  // 1：横屏 0：竖屏
     * closeLike: 0 , // 是否关闭点赞 1：关闭
     * closeGoods: 0, // 是否关闭商品货架，1：关闭
     * closeComment: 0 // 是否开启评论，1：关闭
     * closeReplay: 1 , // 是否关闭回放 1 关闭
     * closeShare: 0,   //  是否关闭分享 1 关闭
     * closeKf: 0, // 是否关闭客服，1 关闭
     */
    public function create()
    {
        $param = $this->request->param();


        return $this->fetch();
    }

    public function createPost()
    {
        $param = $this->request->param();

        $result = $this->validate('$param','Room');

        if ($result !== true) {
            $this->error($result);
        }
        $post = $this->http_post('https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token=',$param);

        if(intval($post['roomId']) > 0 && $post['qrcode_url'] != ''){
            $room = new WxappBroadcastRoomModel();
            $data = [
                'room_id' => $post['roomId'],
                'qrcode_url' => $post['qrcode_url'],
                'create_time' => time()
            ];
            $room->add($data);
        }
        
    }

    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param bool $post_file 是否文件上传
     * @return string content
     */
    private function http_post($url, $param, $post_file=false)
    {
        $oCurl = curl_init();
        if (stripos($url, 'https://') !== false) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {
            $is_curlFile = true;
        } else {
            $is_curlFile = false;
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
            }
        }
        if (is_string($param)) {
            $strPOST = $param;
        } elseif ($post_file) {
            if ($is_curlFile) {
                foreach ($param as $key => $val) {
                    if (substr($val, 0, 1) == '@') {
                        $param[$key] = new \CURLFile(realpath(substr($val, 1)));
                    }
                }
            }
            $strPOST = $param;
        } else {
            $aPOST = [];
            foreach ($param as $key=>$val) {
                $aPOST[] = $key . '=' . urlencode($val);
            }
            $strPOST =  join('&', $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus  = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus['http_code']) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }
}